package com.wn.product.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * @author yellow Docter
 * @date 2022 -11-03
 * @desc
 */
@Component
@Slf4j
public class ConfirmCallBack implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback{

    /**
     * 这个 确定  消息发送到交换机
     *
     * @param correlationData
     * @param ack
     * @param cause
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {

            // correlationData 消息数据
            // ack   true成功
            // cause 失败的原因

            log.info("correlationData 数据:{}",correlationData);

            if (ack){
                log.info("消息发送到了  交换机中.....");
            }else{
                log.info("消息发送到了失败===>{}",cause);
            }

    }

    /**
     * 如果消息发送失败(从交换机到 队列消息出问题) 会执行这个方法
     *
     * @param message
     * @param replyCode
     * @param replyText
     * @param exchange
     * @param routingKey
     */
    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {

            log.info("message==>{}",message);
            log.info("replyCode==>{}",replyCode);
            log.info("replyText==>{}",replyText);
            log.info("exchange==>{}",exchange);
            log.info("routingKey==>{}",routingKey);

            System.out.println("失败的消息：message"+ message);
            //再次发送
            //存储到数据中
            // 异常监控系统 (发送 message)

    }
}
